<template>
  <div>
    <el-table :data="buildings" style="width: 100%">
      <el-table-column prop="address" label="address" width="180" />
      <el-table-column prop="rendNumber" label="rendNumber" width="180" />
      <el-table-column prop="buildingNumber" label="buildingNumber" />
      <el-table-column prop="kilowattHour" label="电费" />
      <el-table-column prop="perTon" label="水费" />

      <el-table-column
          label="操作"
          width="160"
          align="center"
      >
        <template v-slot="scope">
          <el-button
              type="text"
              size="small"
              class="blueBug"
              @click="openDialog(scope.row.id)"
          >
            编辑
          </el-button>
<!--          <el-button-->
<!--              type="text"-->
<!--              size="small"-->
<!--              class="delBut non"-->
<!--&lt;!&ndash;              @click="statusHandle(scope.row)"&ndash;&gt;-->
<!--          >-->
<!--            {{ scope.row.status == '1' ? '禁用' : '启用' }}-->
<!--          </el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <el-dialog v-model="dialogVisible" title="Dialog Title">
      <!-- 使用 el-form 显示描述列表的数据 -->
      <el-form :model="updateParam" label-position="left" label-width="120px">
        <el-form-item label="电费">
          <el-input v-model="updateParam.kilowattHour" />
        </el-form-item><el-form-item label="水费">
          <el-input v-model="updateParam.perTon" />
        </el-form-item>
        <!-- 其他描述列表的项 ... -->
        <el-button type="primary" @click="update" style="margin-top: 30px">确认新增</el-button>
      </el-form>
    </el-dialog>
    <el-pagination
        v-if="buildings.length"
        class="pageList"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="param.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        v-model:current-page="page"
        @current-change="handleCurrentChange"
    ></el-pagination>
  </div>

</template>



<script setup lang="ts">

import {onMounted, reactive, ref} from "vue";
import {
  BaseSetting_,
  BaseSettingControllerService,
  OpenAPI,
  PageParam,
  RoomManageControllerService,
  RoomRequestParam
} from "../../generated";
import {ElMessage} from "element-plus";
import {Plus} from "@element-plus/icons-vue";
const param = reactive({
  current: 1,
  pageSize: 10, // 默认每页显示10条
} as unknown as  PageParam );

const dialogVisible = ref(false);

const openDialog = (id) => {
  console.log(id)
  // 打开弹出框时，将描述列表的数据赋值给 el-form 的数据
  updateParam.id=id;
  updateParam.perTon='';
  updateParam.kilowattHour='';

    // ... 获取其他属性的数据 ...
  dialogVisible.value = true;
};


const handleCurrentChange = (currentPage: number | undefined) => {
  param.current = currentPage;
  getPage();
};
const handleSizeChange = (pageSize: number | undefined) => {
  param.pageSize = pageSize;
  getPage();
};
const buildings=ref([]);
const page = ref(1);
const total=ref(0);
const getPage = async () => {
  try {
    const response = await BaseSettingControllerService.listPageUsingPost(param);
    if (response.code === 200) {
      buildings.value = response.result.list;
      total.value = response.result.total;
      dialogVisible.value = false;
    } else {
      console.error('Failed to fetch rooms:', response.message);
    }
  } catch (error) {
    console.error('Error fetching rooms:', error);
  }
};

onMounted(() => {
  getPage();
});

const updateParam = reactive({
  id: '',
  kilowattHour: '',
  perTon: '',
}) as unknown as  BaseSetting_;
const update=async () => {
  try {
    const response = await BaseSettingControllerService.updateSettingUsingPost(updateParam)
    if (response.code === 200) {
      ElMessage.success("修改成功");
      getPage();
    } else {
      ElMessage.error('修改失败:', response.message);
    }
  } catch (error) {
    console.error('Error fetching rooms:', error);
  }
}
</script>
<style scoped>
.pageList {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
</style>